Skip to content

add support for enabling delete protection - #268

Merged
sudo87 merged 8 commits into
apache:mainfrom
Curverneur:feature/add-resource-protection-support
Aug 17, 2026
Merged

add support for enabling delete protection#268
sudo87 merged 8 commits into
apache:mainfrom
Curverneur:feature/add-resource-protection-support

Conversation

@Curverneur

Copy link
Copy Markdown
Contributor

Description

Add support for enabling the delete protection for instances (virtual machines) and volumes (disks).

Reason

This feature allows users to enable the delete protection for certain resources to prevent accidental deletion.

References

Changes

Added deleteprotection option to cloudstack_instance and cloudstack_disk resource and updated the corresponding documentation pages.

  • modified cloudstack/resource_cloudstack_instance.go
  • modified cloudstack/resource_cloudstack_disk.go
  • modified website/docs/r/instance.html.markdown
  • modified website/docs/r/disk.html.markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds delete protection support for CloudStack instances (virtual machines) and volumes (disks) by introducing a new deleteprotection boolean field to both the cloudstack_instance and cloudstack_disk resources. The feature integrates with CloudStack's API updateVirtualMachine and updateVolume endpoints to enable protection against accidental deletion.

Key Changes:

  • Added deleteprotection optional boolean field to instance and disk resource schemas
  • Implemented delete protection setting via CloudStack update APIs after resource creation
  • Updated documentation for both resources with usage notes about limitations when resources are managed by other services

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
cloudstack/resource_cloudstack_instance.go Added deleteprotection schema field and implementation to set/update protection via UpdateVirtualMachine API
cloudstack/resource_cloudstack_disk.go Added deleteprotection schema field and implementation to set/update protection via UpdateVolume API
website/docs/r/instance.html.markdown Documented deleteprotection parameter with usage notes and reformatted userdata_id/userdata_details entries
website/docs/r/disk.html.markdown Documented deleteprotection parameter with usage notes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cloudstack/resource_cloudstack_disk.go Outdated
Comment thread website/docs/r/disk.html.markdown Outdated
Comment thread cloudstack/resource_cloudstack_disk.go
Comment thread cloudstack/resource_cloudstack_instance.go
Comment thread cloudstack/resource_cloudstack_instance.go Outdated
Curverneur and others added 4 commits December 16, 2025 14:20
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Curverneur

Copy link
Copy Markdown
Contributor Author

looks like some checks failed due to connection issues. could you check and trigger them again, please?

Comment thread cloudstack/resource_cloudstack_disk.go Outdated
Comment thread cloudstack/resource_cloudstack_instance.go Outdated

@vishesh92 vishesh92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Curverneur Thansk for the PR. It looks mostly good. Can you check the comments? It would be great if you can add some tests here as well.

@Curverneur

Copy link
Copy Markdown
Contributor Author

Hi @vishesh92, thanks for the feedback. Changes commited as requested. Does the test meet the requirements? Do you have any suggestions for changes?

@Curverneur

Copy link
Copy Markdown
Contributor Author

Hi @vishesh92, the deleteprotection parameter has been added to the api in version 4.20. Testing with older versions of cloudstack-simulator, e.g. 4.19.3, just won't configure deleteprotection for instances or disks and logs:

WARN  [c.c.a.d.ParamGenericValidationWorker] (qtp2068388745-328:ctx-e860b95a ctx-604302c4 ctx-94a7892a) (logid:8565d729) Received unknown parameters for command updateVirtualMachine. Unknown parameters : deleteprotection

Well, using ExpectError might not be the best approach as indicated in testing.go. Do you have any other ideas? How could I properly test this feature and ensure the test is successful? Had the idea to check the api version and skip the check if the version is older than 4.20, but still not sure where I could get it from.

	// ExpectError allows the construction of test cases that we expect to fail
	// with an error. The specified regexp must match against the error for the
	// test to pass.
	//
	// This functionality is only intended for provider-controlled error
	// messaging. While in certain scenarios this can also catch testing logic
	// error messages, those messages are not protected by compatibility
	// promises.
	ExpectError *regexp.Regexp

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Suppressed comments (4)

cloudstack/resource_cloudstack_instance_test.go:318

  • The expected-error regex ends with an unescaped . which matches any single character, not a literal period. If the actual error message ends exactly with deleted (no trailing character), this regex won’t match and the test will fail. Consider removing the trailing dot or escaping it (e.g., \\.) and/or anchoring/making the pattern more tolerant.
				// attempt to destroy vm. expected to fail due to delete protection is enabled
				Config:      fmt.Sprintf(testAccCloudStackInstance_deleteProtection, true),
				Destroy:     true,
				ExpectError: regexp.MustCompile(".*has delete protection enabled and cannot be deleted."),
			},

cloudstack/resource_cloudstack_disk_test.go:146

  • Same regex issue as the instance test: the unescaped trailing . matches any character, which can cause a false negative if the error string doesn’t have a trailing character. Remove the trailing dot or escape it so the test is robust.
				// attempt to destroy disk. expected to fail due to delete protection is enabled
				Config:      fmt.Sprintf(testAccCloudStackDisk_deleteProtection, true),
				Destroy:     true,
				ExpectError: regexp.MustCompile(".*has delete protection enabled and cannot be deleted."),
			},

cloudstack/resource_cloudstack_instance.go:902

  • Error messaging is inconsistent: Create uses "delete protection" (no underscore) while Update uses "delete_protection" (underscore). For clearer, consistent UX (and easier test matching), standardize the phrasing across create/update (prefer using the user-facing attribute name or a consistent human-readable phrase).
	// Check if the delete protection has changed and if so, update the deleteprotection
	if d.HasChange("delete_protection") {
		p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
		p.SetDeleteprotection(d.Get("delete_protection").(bool))

		_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
		if err != nil {
			return fmt.Errorf(
				"Error updating the delete_protection for instance %s: %s", name, err)
		}
	}

cloudstack/resource_cloudstack_disk_test.go:157

  • Correct spelling in comment from 'disabledd' to 'disabled'.
				// destroy disk. expected to pass due to disk protection is disabledd
				Config:  fmt.Sprintf(testAccCloudStackDisk_deleteProtection, false),

Comment thread cloudstack/resource_cloudstack_instance_test.go Outdated
Comment thread cloudstack/resource_cloudstack_instance.go
Comment thread cloudstack/resource_cloudstack_disk.go
@sudo87
sudo87 force-pushed the feature/add-resource-protection-support branch from d083011 to 67d670e Compare August 17, 2026 06:36
- Read delete_protection back from the API in resourceCloudStackInstanceRead
  and resourceCloudStackDiskRead so the Computed attribute reflects import
  and out-of-band changes instead of always going stale.
- Fix TestAccCloudStackInstance_deleteProtection using the disk CheckDestroy
  helper instead of the instance one.
- Escape the trailing "." in the ExpectError regexes so they match a literal
  period instead of any character.
- Standardize the update error message wording to "delete protection" and
  fix a "disabledd" typo in test comments.
Comment thread cloudstack/resource_cloudstack_disk.go
# Conflicts:
#	cloudstack/resource_cloudstack_instance_test.go

@sureshanaparti sureshanaparti left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clgtm

@sudo87 sudo87 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verification summary — PR #268 against the CloudStack simulator
Automated acceptance tests (ran against apache/cloudstack-simulator:4.22.1.0):
TestAccCloudStackDisk_deleteProtection — PASS (4.95s)
TestAccCloudStackInstance_deleteProtection — PASS (23.96s)

Ran scenarios against simulator

@sudo87
sudo87 merged commit f8e0ca8 into apache:main Aug 17, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants